projectsparkfandomcom-20200223-history
Getting Full Control Over an Object's Movement
Note: this page doesn't fit the Wikia format, and gives indeed only an opinion rather than exact information. So really take it with a grain of salt. '' Spark has a pretty cool physics engine, and has some nice defaults for movement but sometimes you may want to change some of the details of how things work. I'm going to go over some of the common things I can think of for how folks might want to tweak their movement. I always talk about controls with a controller, because that's what I'm familiar with, but everything here should be applicable to any input method. Disclaimer: I don't have access to PS as I write this, so take all kode and tile names with a grain of salt. 'Speed' This is the most common thing you may want to tweak, and luckily is one of the easier ones as well. There are two common ways to change your movement speed. 1) Every object in the game has a movement speed property that defines how fast it moves. You can change this property either in the property menu of in the brains. 2) When you use the move tile, you can specify exactly how fast the move should be with the speed modifier. 'Momentum' Different objects behave differently once they start moving. If you aren't happy with how your character feels, you might want to change their momentum. In order to get full control over your momentum, you can hijack the Velocity of your object. Velocity is a vector, so it gives both a magnitude and a direction. What I did to get a feel of momentum that I liked was that, instead of moving directly when you pushed the left analogue stick, I used the analogue stick to set a "desired velocity" and kept track of my current velocity. Then, each brain iteration, I moved the actual velocity closer to the desired velocity. For the momentum calculation, the kode looked something like this: When [] Do Current Velocity equals Current Velocity times 0.5 plus Desired Velocity times 0.5 When ( Current Velocity minus Desired Velocity ) magnitude than 0.1 Do Current Velocity equals Desired Velocity When [] Do velocity X equals Current Velocity X When [] Do velocity Z equals Current Velocity Z If you tweak the 0.5s in the above kode, make sure the total number always adds up to 1. Changing the ratio will change how much momentum the object has. Something else to note, I like to set the Velocity equal to 0 at the beginning of the brain, then update it based on the left thumbstick. This will make the object start slowing down as soon as you let go of the thumbstick. Also, notice I didn't mess around with the Y component of the velocity above. That needs to be handled separately. 'Fully Controlling Your Jump' Something I like to do for platforming is to get better control over my jump. I'm accustomed to how Mario controls, so I like to get a jump that feels Mario-esque. I think these are the things that make Mario's jump feel as good as it does: 1) The length of time you hold the jump button affects how high you jump. Letting go quickly causes a much smaller jump than holding it down. 2) The faster you are moving, the higher you can jump. This usually works together with the sprint as a pretty important component. 3) There is a little bit of leeway if you press jump "just" before hitting the ground In order to achieve the first goal, you can do some hijacking of the velocity again like I mentioned above. This time though, just focusing on the Y component. First, you need some small jump that is kicked off when you first press jump, then you need to keep increasing your upwards velocity by some amount until jump is released. This takes some fiddling, and I don't have the numbers I ended up liking available at the moment, but the idea is that as soon as you let go of jump, the additional buoyancy is removed. Also, as you hold jump, the amount of the extra buoyancy is constantly decreasing, until it disappears. To jump higher when moving faster, just multiply the amount of buoyancy you are given by 1 + the magnitude of your X and Z velocity (ignoring Y component). You may want to multiply that by some fraction to make it less pronounced. To give you some leeway in re-jumping, what you can do is set a "jump as soon as possible" variable equal to true, but have a timer that sets it back to false after some small period of time. And as soon as you hit the ground, if that variable is true, then jump. I had a stage around getting your jumping to feel right in the alpha. I'll try to get one out for the beta as well. 'Wall Jumping''' Something that is pretty common that isn't in PS by default is wall jumping. What worked well for me was to try to detect any walls right in front of my character, by raycasting a short distance in the forward direction and if jump was pressed when a wall was there, jump in the opposite direction. I had a 2D game with walljumping, so it was a bit simpler, but I think you can ask for the surface normal of any terrain detected with raycasting, so it should be able to work just fine for 3D as well. That's what I can think of for common movement tweaks. Let me know if there are other tweaks that would be worth discussing, or if anything could use some additional clarification.